package com.hao.demo.zoo.config.controller;

import com.hao.demo.zoo.config.properties.OrderProperties;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author Hao
 * @Date 2021/6/2 15:48
 * @Version 2.0
 */
@RefreshScope //刷新，主要使@Value获取到最新数据
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private OrderProperties orderProperties;
    @Autowired
    private DataSourceProperties dataSourceProperties;

    /**
     * 测试 @ConfigurationProperties 注解的配置属性类
     */
    @GetMapping("/test01")
    public OrderProperties test01() {
        return orderProperties;
    }

    @Value(value = "${order.pay-timeout-seconds}")
    private Integer payTimeoutSeconds;
    @Value(value = "${order.create-frequency-seconds}")
    private Integer createFrequencySeconds;

    /**
     * 测试 @Value 注解的属性
     */
    @GetMapping("/test02")
    public Map<String, Object> test02() {
        Map<String, Object> map = new HashMap<>();
        map.put("payTimeoutSeconds", payTimeoutSeconds);
        map.put("createFrequencySeconds", createFrequencySeconds);
        return map;
    }

    @GetMapping("/test")
    public Object test(){
        String url = dataSourceProperties.getUrl();
        return url;
    }
}
